package com.example.baiduapi.Controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.example.baiduapi.Chart.Picther.Sample;
import com.example.baiduapi.Mapper.mysqlMapper.UserMapper;
import com.example.baiduapi.Scoket.websocket.handler.MyWebSocketHandler;
import com.example.baiduapi.Scoket.websocket.service.ChatService;
import com.example.baiduapi.Util.*;
import com.example.baiduapi.domain.AjaxResult;
import com.example.baiduapi.domain.LoginDTO;
import com.example.baiduapi.domain.emnu.PageEnum;
import com.example.baiduapi.pojo.UserInfo;
import com.example.baiduapi.service.LoginService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
@CrossOrigin
@Slf4j
public class testController {

    @Resource
    private RedisUtils redisUtils;
    @Resource
    private MyWebSocketHandler myWebSocketHandler;
    @Resource
    private ChatService chatService;

    @Value("${server.HTTPURL}")
    private String HTTPURL;

    /**
     * 验证码服务
     */
    @Resource
    private zhenziSMS sms;


    @GetMapping("/showPic")
    @ResponseBody
    String shwoPics(String taskId) throws IOException {
        String result = Sample.shwoPic(taskId);
        return result;
    }

    /**
     * 初始化消息
     * @param username 初始化消息人ID ，例1
     * @param token
     * @return
     * @throws Exception
     */
    @PostMapping("/initMessage")
    @ResponseBody
    public String initMessage(String username, String token) throws Exception {
        System.out.println("初始化消息入参为："+username+token);

        if (username.equals("all")){ // 接受消息人为所有
           // 群聊信息暂时不做

        } else {
            // 根据账号去获取该用户要接收和发送的信息[{"name":[{getUser=robot}, {getUser=1}]}, {"data":{robot=[畜FDD亿纹乴乿叫礪, E, E, E, 伔変唾, E, 畜ADD嬣奌奌叫礪, B, 伔変唾, 伔肉倮仯亴丼, 凭乴乞ADD嬣盰案彻, 识乴乞ADD嬣盰筥让], 1=[屻智坜茽坄乾厥珄仲乴桍醥朕ｸ挕贃朑居贄ぶ~辭斂人乹辨奰佔朑墄鞇ｮ⁨噋ｵ郗醥朕晛扥盰ｵ⁩~屻智倨乿腮欑嚪奀乴睿ｸ乴厞朊齔欗窭坜亢盰霖刹ぶ~屻智悾诂坄閚ｮ⁨伔丫佮讀让ｫ⁩~朊齔嚪笠讀ｮ⁨弧煂佮唾ｸ扥厛晛梚柣醸盰嬒靌ぶ⁩~屻智睿眴朊齔痥悥坄讀ｮ⁨郗伔肉乹肉吾讽扥辭桍醥朕晛伔盰ｫ⁩~朊齔搳奀逧ｮ⁨乹肉ｸ嚔乎扥丫乹瞑逧辭晛咞朑盰ぶ⁩, 変盰ｸ扥厛云桍挚惜掤俯盰凱寍哸觵氶朑乎惜畫扤乴箳ADD嬣盰旳窔ぶ惜厞靴觵掤俯乴仯垎杘盰侕怛ｸ俿姶旳窔盰乏飬ふ皚盰ふ醹烍笽ｸ扥居厛云幚勝惜寸扤亏動仲ｵ, B剄M峵晛屻嬒陂毁ｸ辭乞斂偭盰克窑德瑲厥尡辱遫ｸ儃女仲拉谕恩纀肉勯ｸ市彴妿嬒且祊佮覰剭哸逧心桳冲ぶ坜攭胆旍霖ｸ辭乞帀鿰毁盰嬝嬤靴觵揸掕垎破盰讙詴ふ攄嬒哸祊佮禥嬒瞑讲ｸ呸斂丫廠讑沜醹垍儏亢亘盰副達勯ふ戍剐恓恩纀哸覗凇閚飬盰肉勯ぶ~~坜們廃旍霖ｸ寂锋哸攭幼靴觵沜慻嬝嬤盰葑儏搰儑哸辤勜且悛ｸ云砚侩亢亘肉奫們廃扤锋ぶ欐奢ｸ丫觵沜慻侩拐嬝嬤儹厣曀勯ふ歎冸哸儂亢乹舛蠸乎盰弅咹ぶ~~恏盰朑讀ｸB剄M峵盰克窑欗奰仺醹觵盰厥尡陂毁ｸ亢亘靴觵忣剄跇奫盰儇沜哸敛捵ｸ云幚勝亢亘坜呰乞旍霖邉肉奫儜霖厥尡ぶ, 扥肉奫嚪笠閚飬ふ掤俯寮丽ふ覗醾哸庎诚ｸ屲旳杘人乴禹讙詴羏讥扤厒乴禹讙詴ｸ恏级旳杘ｸ畫扤旳杘ｸ凭攱仿ｸ割柤悱纞ｸ掤俯庎诚ｸ彴厥篣没ｸ罢凭亗硵云厾亏伡儂亢垎仺讙詴盰亏動ぶ, 伔変ｵ杽亴丼扥厛云幚勝伔盰呣ｫ]}}]
            String result = chatService.GetInItMessage(username).toString();
            System.out.println(result);
            return JSON.toJSONString(result);
        }
        return null;
    }




    @GetMapping("/test")
    @ResponseBody
    public String test(String username) throws Exception {

       // boolean b = sms.checkMessage("f9b017e1-8f3e-46b8-8fcb-d81e95ee6289", "210309");

        redisUtils.set("16608676369","16608676369");
        String s = redisUtils.get("16608676369");
        redisUtils.expire("16608676369", 10, TimeUnit.SECONDS);

        return s;
    }
    @Async
    @PostMapping("/setRedis/{key}/{value}")
    @ResponseBody
    public String test1(@PathVariable("key") String key,@PathVariable("value") String value) throws Exception {
        try {
            redisUtils.set(key,value);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "1";
    }
    @Async
    @PostMapping("/getRedis/{key}")
    @ResponseBody
    public String test2(@PathVariable("key") String key) throws Exception {
        return String.valueOf(redisUtils.get(key));
    }
    @PostMapping("/deleteRedis/{key}")
    @ResponseBody
    public String test3(@PathVariable("key") String key) throws Exception {
        try {
            redisUtils.delete(key);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return "1";
    }
    public static String getRandomByUUID(){
        String temp = UUID.randomUUID().toString();
        return temp;
    }
    public static void main(String[] args) throws Exception {
        System.out.println(AjaxResult.success("登录成功", 200));
       }
}
